

#import "APGuideView.h"
#import "UIImageView+WebCache.h"

@interface APGuideViewCell ()

@end

@implementation APGuideViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        WS(ws);
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        [self addSubview:imageView];
        self.imageView = imageView;
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(ws).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
        
    }
    return self;
}



@end

@interface APGuideView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation APGuideView


NSString * const cellIdentifier = @"DMMarketingGuideViewCell";

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _dataArray = [NSArray arrayWithObjects:
                      [UIImage imageNamed:@"guide0"],
                      [UIImage imageNamed:@"guide1"],
                      [UIImage imageNamed:@"guide2"],
                      nil];
        WS(ws);
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.sectionInset = UIEdgeInsetsZero;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:layout];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.alwaysBounceHorizontal = NO;
        collectionView.alwaysBounceVertical = NO;
        collectionView.bounces = NO;
        collectionView.pagingEnabled = true;
        [collectionView registerClass:[APGuideViewCell class] forCellWithReuseIdentifier:cellIdentifier];
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        [self addSubview:collectionView];
        self.collectionView = collectionView;
        [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(ws).with.insets(UIEdgeInsetsZero);
        }];
        
    }
    return self;
}

- (void)setGuides:(NSArray *)guides{
    _guides = guides;
    [self.collectionView reloadData];
}

- (void)goPage:(NSInteger)page{
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:page inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:true];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.guides.count) {
        return self.guides.count;
    }else{
        return self.dataArray.count;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.frame.size.width, self.frame.size.height);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    APGuideViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    if (self.dataArray.count) {
        cell.imageView.image = [self.dataArray objectAtIndex:indexPath.item];
    }
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(guideView:didSelectedItem:)]) {
        [self.delegate guideView:self didSelectedItem:indexPath];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if ([self.delegate respondsToSelector:@selector(guideViewDidEndDragging:)]) {
        [self.delegate guideViewDidEndDragging:self];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger page = (NSInteger)(scrollView.contentOffset.x / scrollView.bounds.size.width);
    self.currentPage = page;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger page = (NSInteger)(scrollView.contentOffset.x / scrollView.bounds.size.width);
    self.currentPage = page;
}

@end
